﻿
using System.Collections.Generic;
using DevExpress.XtraGrid.Views.Grid;
namespace HuaCheng.Controller
{
    public class ListMethod
    {
        public delegate void UpdateGridViewDelegate();

        public delegate void MsgBoxShowDelegate();
        /// <summary>
        /// 上移列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="del"></param>
        /// <param name="gridView"></param>
        public static void MoveUpList<T>(List<T> list, UpdateGridViewDelegate del, GridView gridView)
        {
            int index = gridView.FocusedRowHandle;
            if (index > 0 && index < list.Count)
            {
                T temp = list[index];
                list.Insert(index - 1, temp);
                list.RemoveAt(index + 1);
                if (del != null)
                    del.Invoke();
                gridView.FocusedRowHandle = index - 1;
            }
        }
        /// <summary>
        /// 下移列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="del"></param>
        /// <param name="gridView"></param>
        public static void MoveDownList<T>(List<T> list, UpdateGridViewDelegate del, GridView gridView)
        {
            int index = gridView.FocusedRowHandle;
            if (index > 0 && index < list.Count - 1)
            {
                T temp = list[index];
                list.Insert(index + 2, temp);
                list.RemoveAt(index);
                if (del != null)
                    del.Invoke();
                gridView.FocusedRowHandle = index + 1;
            }
        }
        /// <summary>
        /// 添加项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="t"></param>
        /// <param name="del"></param>
        public static void AddItem<T>(List<T> list, T t, UpdateGridViewDelegate del)
        {
            list.Add(t);
            if (del != null)
                del.Invoke();
        }
        /// <summary>
        /// 删除当前项
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="del"></param>
        /// <param name="gridView"></param>
        public static void DeleteItem<T>(List<T> list, UpdateGridViewDelegate del, GridView gridView)
        {
            int index = gridView.FocusedRowHandle;
            if (index < list.Count && index >= 0)
            {
                list.RemoveAt(index);
                if (del != null)
                    del.Invoke();
                gridView.FocusedRowHandle = index - 1;
            }
        }
    }
}
